﻿
/*----------------------------------------------------------------
 * 创建时间：2023/8/22 10:51:44
 * 作  者：wenlli 
 * 项  目：Minotaur.App.Core 
 * 描  述：数据库事务相关操作
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时  间：
 * 修改说明：
 * 
 *----------------------------------------------------------------*/


using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minotaur.App.Core
{
    public static class SqlSugarTransactionCommon
    {
        /// <summary>
        /// 同步处理单库事务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sqlSugar"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static DbResult<T> UseSugarTran<T>(this ISqlSugarClient sqlSugar, Func<T> action)
        {
            DbResult<T> dbResult = new DbResult<T>() { IsSuccess = true };
            try
            {
                sqlSugar.AsTenant().BeginTran();
                dbResult.Data = action();
                sqlSugar.AsTenant().CommitTran();
            }
            catch (Exception ex)
            {
                sqlSugar.AsTenant().RollbackTran();
                dbResult = new DbResult<T>() { ErrorException = ex, ErrorMessage = ex.Message, IsSuccess = false, Data = default(T) };
            }
            return dbResult;
        }

        /// <summary>
        /// 同步处理单库事务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sqlSugar"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static async Task<DbResult<T>> UseSugarTranAsync<T>(this ISqlSugarClient sqlSugar, Func<Task<T>> action)
        {
            DbResult<T> dbResult = new DbResult<T>() { IsSuccess = true };
            try
            {
                await sqlSugar.AsTenant().BeginTranAsync();
                var actionRes = await action();
                dbResult.Data = actionRes;
                await sqlSugar.AsTenant().CommitTranAsync();
            }
            catch (Exception ex)
            {
                await sqlSugar.AsTenant().RollbackTranAsync();
                dbResult = new DbResult<T>() { ErrorException = ex, ErrorMessage = ex.Message, IsSuccess = false };
            }
            return dbResult;
        }

        /// <summary>
        /// 同步处理多库事务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sqlSugar"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static DbResult<T> UseMoreSugarTran<T>(this SqlSugarClient sqlSugar, Func<T> action)
        {
            DbResult<T> dbResult = new DbResult<T>() { IsSuccess = true };
            try
            {
                sqlSugar.BeginTran();
                dbResult.Data = action();
                sqlSugar.CommitTran();
            }
            catch (Exception ex)
            {
                sqlSugar.RollbackTran();
                dbResult = new DbResult<T>() { ErrorException = ex, ErrorMessage = ex.Message, IsSuccess = false, Data = default(T) };
            }
            return dbResult;
        }

        /// <summary>
        /// 同步处理多库事务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sqlSugar"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static async Task<DbResult<T>> UseMoreSugarTranAsync<T>(this SqlSugarClient sqlSugar, Func<Task<T>> action)
        {
            DbResult<T> dbResult = new DbResult<T>() { IsSuccess = true };
            try
            {
                await sqlSugar.BeginTranAsync();
                var actionRes = await action();
                dbResult.Data = actionRes;
                await sqlSugar.CommitTranAsync();
            }
            catch (Exception ex)
            {
                await sqlSugar.RollbackTranAsync();
                dbResult = new DbResult<T>() { ErrorException = ex, ErrorMessage = ex.Message, IsSuccess = false };
            }
            return dbResult;
        }
    }
}
